<?php include_once __DIR__.'/../inc/head.html';?>
<div class='fixed hwrap'>
		<a href='javascript:history.back(-1)' target='_parent' class='rel'>
			<i class='icon-angle-left'></i>
		</a>
		<a href='javascript:;' target='_parent'></a>
		<h3 class='dyc'>我的购物车</h3>
	</div>
	<div style='height:45px;'></div>
	<div class="m_goods">
	<ul id="mBuyCarList">
        <?php foreach($cart_list as $k => $v):?>
		<li mBuyCarLi='83555294434159'>
			<div class='mBuyCarSeller'>平台自营</div>
			<div class='mBuyCarGoods'>
				<input type='checkbox' onclick="change_cart_total_price()" class="selectorCheck" switchbuycar="single" value="<?=$v['id']?>" checked>
				<a href='/goods/detail?gid=<?=$v['id']?>'><img class='mBuyCarIco' src='<?=$v['pic']?>'></a>
				<a href='/goods/detail?gid=<?=$v['id']?>'><p class='mBuyCarGoodsName'><?=$v['goods_name']?></p></a>
				<a href='/goods/detail?gid=<?=$v['id']?>'><p class='mBuyCarGoodsSkuName'><?=$v['title']?></p></a>
				<p class='mBuyCarGoodsId'>运费：<span id='postage_<?=$v['id']?>' data-num='<?=$v['nums']?>' data-postage='<?=$v['postage']?>'><?=$v['postage']?></span></p>
				<div singlePrice='83555294434159'id="total_price_<?=$v['id']?>">￥<?=$v['total_price']?></div>
				<div class='mBuyCarNum' onclick="change_cart_goods_nums('<?=$v['id']?>', 'cut')">-</div>
				<input type="hidden" value="<?=$v['price']?>"  id="price_<?=$v['id']?>"/>
				<input type='text' id="cartNums_<?=$v['id']?>" class='mBuyCarNum' numBuyCarId='83555294434159' value="<?=$v['nums']?>">
				<div class='mBuyCarNum' onclick="change_cart_goods_nums('<?=$v['id']?>', 'add');">+</div>
				<a onclick="del_cart_goods('<?=$v['cart_id']?>')" class='mBuyCarDel'>删除<a>
				<div class='clear'></div>
			</div>
		</li>
        <?php endforeach;?>
	</ul>
</div>
<div style="height:60px"></div>
<div class="payBar fixed">
	<label><input type="checkbox" switchBuyCar="allCheck" onclick="selectAll()" class="clicked" checked>&nbsp;全选</label>&nbsp;&nbsp;
    已选商品：<em class="ctrlKey" id="buyCarSum"><?=$cart_count?></em>件
    <span class="floatRight">合计：<em class="key" id="allMoney">￥<?=$cart_total_price?></em></span>&nbsp;&nbsp;
    <a href="javascript:;" onclick="$('#usBuyCarPayDiv').fadeIn()" class="alipay" id="BuyCarBalance">结算</a>
</div>
<!--支付确认弹出层开始-->
<div class="scroll_box hide" id="usBuyCarPayDiv">
    <div class="fixed hwrap" style="border-bottom:0;"><a href="javascript:;" onclick="$('#usBuyCarPayDiv').hide()" class="rel"><i class="icon-angle-left"></i></a>
      <h3 class="dyc">确认订单</h3>
    </div>
    <div style="height:45px;"></div>
    <ul class="wrap">
        <li class="one">
            <a href="/user/useraddr">
                <dl>
                    <dt>
						<input type="hidden" id="address_id" value="<?=isset($address['id'])?$address['id']:0; ?>" />
						<span class="str"><?=isset($address['address_name'])?$address['address_name']:''; ?></span>
						<span class="str"><?=isset($address['address_phone'])?$address['address_phone']:''; ?></span>
						<em class="defalut">默认</em>
					</dt>
                    <dd><?=isset($address['address'])?$address['address']:''; ?></dd>
                </dl>
                <i class="icon-angle-right"></i>
            </a>
        </li>
        <li>
            账户余额：
            <span class="fr key">￥<?=$userinfo['money']?></span>
        </li>
		<li>
            支付方式：
            <span class="fr key">余额支付</span>
        </li>
    </ul>
    <div class="bottom">
        <span class="fl">还需支付：<em class="key" id="payId"><?=$cart_total_price-$userinfo['money']>0?$cart_total_price-$userinfo['money']:0?></em></span>
        <a href="javascript:;" onclick="pay_cart_goods()">立即支付</a>
    </div>
</div>
<div class="hide">

</div>
<!--支付确认弹出层结束-->

	<div class='hide' id='warn'>
	    <div class='dibian' style='z-index:100'></div>
		<div class='win' style=' width:300px;height:160px; margin:-80px 0 0 -150px;z-index:101;'>
			<p class='winTitle'>温馨提示<span onclick="$('#warn').hide()" class='winClose'>×</span></p>
			<div id='warnWord'>无</div>
			<div id='warnSure' onclick="$('#warn').hide()">确定</div>
			<div id='warnCancel' onclick="$('#warn').hide()">取消</div>
		</div>
	</div>
    <script>
        $(function(){
            clearInterval();
        })
        function change_cart_goods_nums(id,type){
            var num = $("#cartNums_"+id).val();
            if(type == "cut"){
        		if(num <= 1){
        			warn("不能再少了");
        		}else{
        			num--;
        		}
        	}else if(type == "add"){
        		if(num >= 99){
        			warn("购买量不能超过最大库存");
        		}else{
        			num++;
        		}
        	}
            $("#cartNums_"+id).val(num);
            var postage = $("#postage_"+id).data("postage");//运费
            var total_postage = postage*num;//总运费
            //修改自己商品价格
            var price = parseFloat($("#price_"+id).val());
            var total_price = price*num+total_postage;//商品总价格
            $("#total_price_"+id).html("￥"+total_price);
            //运费重新赋值
            $("#postage_"+id).html(total_postage);
            change_cart_total_price();
        }
        //修改购物总数及总价
        function change_cart_total_price(){
            var cart_total_price = 0;//购物车总价
            var cart_count = 0;//商品件数
            var user_money = <?=$userinfo['money']?>;//用户余额
            var total_postage = 0; //总运费
            var cart_num_count = 0; //购物车总件数
            //循环选中的购物车商品
            $("#mBuyCarList").find(":checkbox:checked").each(function(index,obj){
                var cart_id = $(obj).val();
                cart_total_price += parseFloat($("#total_price_"+cart_id).html().replace("￥",""));
                cart_num_count += parseInt($("#cartNums_"+cart_id).val());
                total_postage += parseFloat($("#postage_"+cart_id).html());
                cart_count++;
            })
            var postage_limit = <?=5?>;
            var shop_postage = <?=10?>;
            //如果订单商品超出设置，运费叠加上店铺设置运费
            if(cart_num_count>postage_limit){
                total_postage += shop_postage;
                cart_total_price += shop_postage;
            }
            $("#shop_postage").html(total_postage);
            $("#buyCarSum").html(cart_count);
            $("#allMoney").html("￥"+cart_total_price);
            var pay_price = cart_total_price-user_money;
            if(pay_price<0)pay_price=0;
            $("#payId").html(pay_price);

        }
        //反选
        function selectAll(){
            $("#mBuyCarList :checkbox").each(function(){
                var state = $(this).prop('checked');
                $(this).prop('checked',!state);
            });
            change_cart_total_price();
        }
        function del_cart_goods(id){
            layer.confirm('确认要删除购物车商品吗?', function(index){
                $.ajax({
                    url:"/user/delCartGoods",
                    method:"post",
                    dataType:"json",
                    data:{id:id},
                    success:function(res){
                        my.action(res);
                        // layer.close(index);
                    }
                });
            });
        }
        //结算支付购物车选中订单
        function pay_cart_goods(){
            var address_id = $("#address_id").val();
            var goods_ids = {};
            $("#mBuyCarList").find(":checkbox:checked").each(function(i,e){
                var nums = $(e).parent().parent().find("input.mBuyCarNum").val();
                goods_ids[$(e).val()] = nums;
            })
            var payPrice = parseFloat($("#payId").html());
            $.ajax({
                url:"../order/createOrder",
                method:"post",
                dataType:"json",
                data:{goods_ids:goods_ids,address_id:address_id,payPrice:payPrice},
                success:function(res){
                    if(res.data.length  != 0){
                        my.action(res);
                    }else{
                        my.action(res);
                        // layer.close(index);
                    }

                }
            });
        }
        function checkRecharge(order_no,order_goods_no){
            console.log(order_no);
            console.log(order_goods_no);
            return;

        }
        //选择收货地址
        function change_user_address(address_id){
            $.ajax({
                url:"/user/getUserAddress",
                method:"post",
                dataType:"json",
                data:{id:address_id},
                success:function(res){
                    $("#sh_name").html(res.data.address_name);
                    $("#sh_phone").html(res.data.address_phone);
                }
            });
        }
    </script>
